//
//  AppeptNewGroupVC.m
//  NtoccShipper
//
//  Created by mac on 2019/11/19.
//  Copyright © 2019 zhuchongjin. All rights reserved.
//

#import "AppeptNewGroupVC.h"
#import "HCSStarRatingView.h"
#import "DriverListVC.h"
#import "DriverListModel.h"

@interface AppeptNewGroupVC ()
@property (weak, nonatomic) IBOutlet UITextField *fieldName;
@property (weak, nonatomic) IBOutlet UIButton *btnSelect;
@property (weak, nonatomic) IBOutlet UIButton *btnSave;
@property (nonatomic,copy) NSString *strCodes;
@property (weak, nonatomic) IBOutlet UILabel *labGroupName;
@property (weak, nonatomic) IBOutlet UILabel *labGroupMembers;
@end

@implementation AppeptNewGroupVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitleName:@"新增承运分组"];
    [self setBackLeftButton:@""];
    
    self.labGroupName.textColor = [UIColor cjMainTextColor];
    self.labGroupMembers.textColor = [UIColor cjMainTextColor];
    self.btnSave.backgroundColor = [UIColor cjMainColor];
    [self.btnSelect setTitleColor:[UIColor cjSubheadTextColor] forState:UIControlStateNormal];
    
    NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:@"请填写分组名称" attributes:@{NSForegroundColorAttributeName:[UIColor cjSubheadTextColor],NSFontAttributeName:self.fieldName.font}];
    self.fieldName.attributedPlaceholder = attrString;
                                                                                                
    self.strCodes = @"";
    [self setFieldLimit];
}
- (void)setFieldLimit{
    self.fieldName.textFieldType = CJTextFieldStringTypeNone;
    self.fieldName.maxLength = CJTextLengthSixtyFour;
}
- (IBAction)btnSaveAction:(id)sender {
    
    if ([CJRegular getIsStrEmpty:self.fieldName.text]) {
        [CJAlert showStringDismissWithDelay:@"请填写分组名称"];
    }else if ([CJRegular getIsStrEmpty:self.strCodes]){
        [CJAlert showStringDismissWithDelay:@"请选择分组成员"];
    }else{
        
        NSString *strURL = CJStringWithFormat(@"%@/ntocc-basic-api/vehicleTeam/shipment",CJBaseUrl);
        NSDictionary *dicPar = @{@"name":self.fieldName.text,
                                 @"driverCodes":self.strCodes,
        };
        
        WS(ws);
        [[HDNetworking sharedHDNetworking] POST:strURL parameters:dicPar success:^(id  _Nonnull responseObject) {
            
//            DLog(@"++++++++%@",responseObject);
            if (ws.blockSuccess) {
                ws.blockSuccess(@"");
            }
            [ws pop];
            
        } failure:^(NSError * _Nonnull error) {
            DLog(@"++++++++%@",error);
            
        }];
    }
}

- (IBAction)btnSelectAction:(id)sender {
    WS(ws);
    DriverListVC *vc = [DriverListVC new];
    vc.blockSeletArr = ^(id anyObject) {
        DLog(@"====== %@",anyObject);
        NSArray *list = (NSArray *)anyObject;
        
        __block NSString *strName = @"";
        __block NSString *codes = @"";
        [list enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            DriverListModel *model= obj;
            if (idx == 0) {
                strName = model.name;
                codes = model.partyCode;
            }else{
                strName = CJStringWithFormat(@"%@,%@",strName,model.name);
                codes = CJStringWithFormat(@"%@,%@",codes,model.partyCode);
            }
            [ws.btnSelect setTitleColor:[UIColor cjMainTextColor] forState:UIControlStateNormal];
        }];
        
        [ws.btnSelect setTitle:strName forState:UIControlStateNormal];
        ws.strCodes = codes;
        
    };
    [self pushVc:vc];

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
